import { Component, OnInit, ViewChild, TemplateRef } from '@angular/core';
import { NzMessageService, NzModalService } from 'ng-zorro-antd';
import { _HttpClient, ModalHelper } from '@delon/theme';
import { STComponent, STColumn, STData } from '@delon/abc';
import { CostOfflineEditComponent } from './edit/edit.component';
@Component({
  selector: 'cost-offline',
  templateUrl: './offline.component.html',
  styles: [`
    .search__form {
      margin-bottom: 0;
    }
  `]
})
export class CostOfflineComponent implements OnInit {
  queryStr: any = {
    offset: 0,
    limit: 10,
    type: '线下'
  };
  data: any[] = [];
  loading = false;
  @ViewChild('st')
  st: STComponent;
  columns: STColumn[] = [
    { title: '标题', index: 'title' },
    { title: '类型', index: 'type' },
    { title: '金额', index: 'money' },
    { title: '状态', index: 'status' },
    { title: '去向', index: 'opposite' },
    { title: '备注', index: 'remark' },
    { title: '时间', index: 'createAt' }
  ];
  selectedRows: STData[] = [];
  description = '';
  totalCallNo = 0;
  expandForm = false;
  constructor(
    private http: _HttpClient,
    public msg: NzMessageService,
    private modal: ModalHelper
  ) {}
  ngOnInit() {
    this.getData();
  }
  getData() {
    this.http.get('api/records/list', this.queryStr).subscribe((res: any) => {
      this.data = res.objects;
      this.loading = false;
    });
  }
  reset(ls: any[]) {
    for (const item of ls) item.value = false;
    this.getData();
  }
  // 编辑新增
  openEdit(i: any = {}) {
    this.modal.create(CostOfflineEditComponent, { i }, { size: 'md' })
      .subscribe(res => {
        if(i._id) {
          res['_id'] = i._id;
          this.http.patch('api/records/update/', res).subscribe((res: any) => {
            if(res.success) {
              this.msg.success(`修改成功`);
              this.getData();
            }else{
              this.msg.error(`已存在`);
            }
          }, () => {
            this.msg.error(`网络或服务器错误`);
          });
        }else {
          this.http.post('api/records/add/', res).subscribe((res: any) => {
            if(res.success) {
              this.msg.success(`添加成功`);
              this.getData();
            }else{
              this.msg.error(`已存在`);
            }
          }, () => {
            this.msg.error(`网络或服务器错误`);
          });
        }
      });
  }
}
